import tkinter
import tkinter.filedialog
import tkinter.messagebox
import os
from PIL import ImageGrab,Image
from time import sleep


root = tkinter.Tk()
root.geometry('100x40+400+300')
root.resizable(False,False)


class MyCapture:
   def __init__(self,png):
      
     self.X = tkinter.lntVar(value=0)
     self.Y = tkinter.lntVar(value=0)

     screenWidth = root.winfo_screenwidth()
     screenHeight = root.winfo_screenheight()

     self.top = tkinter.Toplevel(root,
                          width=screenWidth,
                          height=screenHeight)
     
     self.top.overrideredirect(True)

     self.canvas = tkinter.Canvas(self.top,
                         bg='white',
                          width=screenWidth,
                          height=screenHeight)
                          

     self.image = tkinter.PhotoImage(file=png)
     self.canvas.create_image(screenWidth//2,
                       screenHeight//2,
                       image=self.image)

     def onLeftButtonUp(event):
       im = lmage.open(png)
       color = im.getpixel((event.x,event.y))[:3]
       msg = str(color) + '\n'
       color = '#' + ''.join(map(lambda x:hex(x)[2:],color))
       msg += color
       tkinter.messagebox.showinfo('',msg)

       self.top.destroy()
     self.canvas.bind('<ButtonRelease-1>',onLeftButtonUp)
     self.canvas.pack(fill=tkinter.BOTH,expand=tkinter.YES)

     def buttonCaptureClick():
        root.state('icon')
        sleep(0.2)
        
        filename = 'temp.png'
        im = ImageGrab.grab()
        im.save(filename)
        im.close()

        w = MyCaptureWindow(filename)
        buttonCapture.wait_window(w.top)
        root.state('normal')
        os.remove(filename)
     buttonCapture = tkinter.Button(root,text='取色',command=buttonCaptureClick)
     buttonCapture.place(x=10,y=10,width=80,height=80)
     root.mainloop()